package com.start.pay.core.event;

import com.start.common.tools.SpringContextTools;
import com.start.common.tools.StartMapTools;
import com.start.pay.core.entity.StartPayOrderEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class GlobalStartPayOrderEventDispatcher implements StartPayOrderEvent {

    private static final GlobalStartPayOrderEventDispatcher dispatcher = new GlobalStartPayOrderEventDispatcher();

    private HashMap<String, List<StartPayOrderEvent>> businessEventMap;
    private HashMap<String, List<StartPayOrderEvent>> appIdEventMap;
    private HashMap<String, List<StartPayOrderEvent>> orderTagsEventMap;
    private HashMap<String, List<StartPayOrderEvent>> platformTypeEventMap;

    private List<StartPayOrderEvent> globalEvent;
    private volatile boolean isInit = false;

    public static GlobalStartPayOrderEventDispatcher getInstance() {
        if (dispatcher.isInit) {
            return dispatcher;
        }
        synchronized (GlobalStartPayOrderEventDispatcher.class) {
            if (dispatcher.isInit) {
                return dispatcher;
            }
            dispatcher.init();
            dispatcher.isInit = true;
        }
        return dispatcher;
    }

    private void init() {
        businessEventMap = new HashMap<>();
        appIdEventMap = new HashMap<>();
        platformTypeEventMap = new HashMap<>();
        orderTagsEventMap = new HashMap<>();
        globalEvent = new ArrayList<>();
        Map<String, StartPayOrderEvent> beans = SpringContextTools.getBeans(StartPayOrderEvent.class, false);
        if (beans == null || beans.isEmpty()) return;
        for (StartPayOrderEvent event : beans.values()) {
            if (StringUtils.isNotEmpty(event.appId())) {
                StartMapTools.mapAddValue(appIdEventMap, event, event.appId(), event.SPLIT());
                continue;
            }
            if (StringUtils.isNotEmpty(event.businessType())) {
                StartMapTools.mapAddValue(businessEventMap, event, event.businessType(), event.SPLIT());
                continue;
            }
            if (StringUtils.isNotEmpty(event.orderTags())) {
                StartMapTools.mapAddValue(orderTagsEventMap, event, event.orderTags(), event.SPLIT());
                continue;
            }
            if (StringUtils.isNotEmpty(event.platformType())) {
                StartMapTools.mapAddValue(platformTypeEventMap, event, event.platformType(), event.SPLIT());
                continue;
            }
            globalEvent.add(event);
        }

    }

    @Override
    public void onCreateOrderBefore(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCreateOrderBefore(entity);
                }
            }
        }

        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onCreateOrderBefore(entity);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCreateOrderBefore(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCreateOrderBefore(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onCreateOrderBefore(entity);
        }
    }

    @Override
    public void onCreateOrderAfter(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCreateOrderAfter(entity);
                }
            }
        }
        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onCreateOrderAfter(entity);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCreateOrderAfter(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCreateOrderAfter(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onCreateOrderAfter(entity);
        }
    }

    @Override
    public void onCancelOrderBefore(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCancelOrderBefore(entity);
                }
            }
        }
        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onCancelOrderBefore(entity);
                    }
                }
            }
        }
        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCancelOrderBefore(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCancelOrderBefore(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onCancelOrderBefore(entity);
        }
    }

    @Override
    public void onCancelOrderAfter(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCancelOrderAfter(entity);
                }
            }
        }
        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onCancelOrderAfter(entity);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCancelOrderAfter(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onCancelOrderAfter(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onCancelOrderAfter(entity);
        }
    }

    @Override
    public void onPayOrderBefore(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderBefore(entity);
                }
            }
        }
        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onPayOrderBefore(entity);
                    }
                }
            }
        }
        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderBefore(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderBefore(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onPayOrderBefore(entity);
        }
    }

    @Override
    public void onPayOrderAfter(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderAfter(entity);
                }
            }
        }
        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onPayOrderAfter(entity);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderAfter(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderAfter(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onPayOrderAfter(entity);
        }
    }

    @Override
    public void onPayOrderSuccess(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderSuccess(entity);
                }
            }
        }
        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onPayOrderSuccess(entity);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderSuccess(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderSuccess(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onPayOrderSuccess(entity);
        }
    }

    @Override
    public void onPayOrderError(StartPayOrderEntity entity, Object error) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderError(entity, error);
                }
            }
        }
        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onPayOrderError(entity, error);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderError(entity, error);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onPayOrderError(entity, error);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onPayOrderError(entity, error);
        }
    }

    @Override
    public void onOrderRefundBefore(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundBefore(entity);
                }
            }
        }
        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onOrderRefundBefore(entity);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundBefore(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundBefore(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onOrderRefundBefore(entity);
        }
    }

    @Override
    public void onOrderRefundAfter(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundAfter(entity);
                }
            }
        }
        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onOrderRefundAfter(entity);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundAfter(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundAfter(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onOrderRefundAfter(entity);
        }
    }

    @Override
    public void onOrderRefundSuccess(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundSuccess(entity);
                }
            }
        }

        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onOrderRefundSuccess(entity);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundSuccess(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundSuccess(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onOrderRefundSuccess(entity);
        }
    }

    @Override
    public void onOrderRefundError(StartPayOrderEntity entity, Object error) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundError(entity, error);
                }
            }
        }

        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onOrderRefundError(entity, error);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundError(entity, error);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderRefundError(entity, error);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onOrderRefundError(entity, error);
        }
    }

    /**
     * 交易关闭
     *
     * @param entity
     */
    @Override
    public void onOrderClose(StartPayOrderEntity entity) {
        String businessType = entity.getBusinessType();
        if (StringUtils.isNotEmpty(businessType)) {
            List<StartPayOrderEvent> events = businessEventMap.get(businessType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderClose(entity);
                }
            }
        }

        String orderTags = entity.getTags();
        if (StringUtils.isNotEmpty(orderTags)) {
            List<String> tags = new ArrayList<>();
            if (orderTags.contains(SPLIT())) {
                tags = Arrays.stream(orderTags.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            } else {
                tags.add(orderTags);
            }
            for (String tag : tags) {
                List<StartPayOrderEvent> events = orderTagsEventMap.get(tag);
                if (events != null && !events.isEmpty()) {
                    for (StartPayOrderEvent event : events) {
                        event.onOrderClose(entity);
                    }
                }
            }
        }

        String appId = entity.getAppId();
        if (StringUtils.isNotEmpty(appId)) {
            List<StartPayOrderEvent> events = appIdEventMap.get(appId);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderClose(entity);
                }
            }
        }
        String platformType = entity.getPlatformType();
        if (StringUtils.isNotEmpty(platformType)) {
            List<StartPayOrderEvent> events = platformTypeEventMap.get(platformType);
            if (events != null && !events.isEmpty()) {
                for (StartPayOrderEvent event : events) {
                    event.onOrderClose(entity);
                }
            }
        }
        if (globalEvent.isEmpty()) return;
        for (StartPayOrderEvent event : globalEvent) {
            event.onOrderClose(entity);
        }
    }

    /**
     * 发生严重错误
     *
     * @param error
     */
    @Override
    public void onError(Object error) {
        log.error("订单异常：", error);
    }
}
